﻿Friend Class TableDefinitionReader
    Public Shared Function Read(t As Type, colCreater As Action(Of Type, Action(Of ColumnDefinitionAttribute))) As TableDefinition
        Dim def As New TableDefinition
        def.Table = TableAttributeReader.Read(t)
        def.ColumnDefinisions.AddRange(ReadColumn(t, colCreater))

        '#72「DDL処理時、和名を設定しておきたい」
        def.DisplayAttribute = AttributeReader.Read(Of ClassDisplayAttribute)(t).FirstOrDefault
        If def.Table.IsRootBase = False Then
            def.SuperDisplayAttribute = AttributeReader.Read(Of ClassDisplayAttribute)(def.Table.SuperRelations.First.YourColumn.Table.ModelType).FirstOrDefault
        End If
        Return def
    End Function

    Private Shared Function ReadColumn(t As Type, colCreater As Action(Of Type, Action(Of ColumnDefinitionAttribute))) As IEnumerable(Of ColumnDefinitionAttribute)
        '再利用することはまれのため、キャッシュはしません。
        Dim lst As New List(Of ColumnDefinitionAttribute)
        colCreater.Invoke(t, Sub(item) lst.Add(item))
        Return lst
    End Function
End Class